//
//  ZyaFeedOrderAddAmountCell.swift
//  SlaughterClient
//
//  Created by Zhouweibin on 2018/1/26.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit

class ZyaFeedOrderAddAmountCell: UITableViewCell {
    
    var isShow = false {
        didSet {
            priceLabel.isHidden = !isShow
        }
    }
    
    var model: ZyaFeedOrder? {
        didSet {
            productLabel.text = model?.brandNm
            amountTf.text = model?.amount
       
            if let price = model?.brandPrice, let unit = model?.brandUnit {
                priceLabel.text = "\(price)/\(unit)"
                unitLabel.text = unit
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = Theme.backgroudColor
        selectionStyle = .none
        
        self.contentView.addSubview(bgView)
        
        bgView.addSubview(productLabel)
        bgView.addSubview(unitLabel)
        bgView.addSubview(amountTf)
        bgView.addSubview(priceLabel)
        
        amountTf.delegate = self
        setupConstraints()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupConstraints () {
        
        bgView.snp.makeConstraints { (maker) in
            maker.edges.equalTo(UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8))
            maker.height.greaterThanOrEqualTo(44)
        }
        
        productLabel.snp.makeConstraints { (maker) in
            maker.left.equalToSuperview().offset(8)
            maker.centerY.equalToSuperview()
        }
        
        unitLabel.snp.makeConstraints { (maker) in
            maker.right.equalToSuperview()
            maker.width.equalTo(40)
            maker.centerY.equalToSuperview()
        }
        
        priceLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(productLabel.snp.right).offset(4)
            maker.right.equalTo(amountTf.snp.left).offset(-4)
            maker.width.equalTo(60)
            maker.centerY.equalToSuperview()
        }
        
        amountTf.snp.makeConstraints { (maker) in
            maker.right.equalTo(unitLabel.snp.left).offset(-4)
            maker.size.equalTo(CGSize(width: 60, height: 30))
            maker.centerY.equalToSuperview()
        }
    }
    
    lazy var amountTf: UITextField = {
        let textfield = UITextField()
        textfield.layer.borderColor = Theme.primaryLightColor.cgColor
        textfield.layer.borderWidth = 1
        textfield.textAlignment = .center
        textfield.keyboardType = .numberPad
        return textfield
    } ()

    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    } ()
    
    lazy var productLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.text = "222-YN-SN"
        label.numberOfLines = 0
        label.font = UIFont.boldSystemFont(ofSize: 14)
        return label
    } ()
    
    lazy var unitLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.text = "包"
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 12)
        return label
    } ()
    
    lazy var priceLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.numberOfLines = 0
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 15)
        return label
    } ()

}

extension ZyaFeedOrderAddAmountCell: UITextFieldDelegate {
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        model?.amount = textField.text!
    }
    
}
